package com.es.www.kyl.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author mct
 * @date 2024/12/12
 * description
 */
@Component
public class RedisUtils {

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	public <T> void setCacheMap(String key, Map<String, T> dataMap) {
		if (dataMap != null) {
			redisTemplate.opsForHash().putAll(key, dataMap);
		}
	}

	/**
	 * 设置缓存过期时间
	 * @param key
	 * @param time
	 */
	public void expire(String key, long time, TimeUnit timeUnit) {
		redisTemplate.expire(key, time, timeUnit);
	}

	/**
	 * 获取String中的数据
	 * @param key
	 * @return
	 */
	public String getStringValue(String key) {
		return redisTemplate.opsForValue().get(key);
	}

	/**
	 * 判断key是否存在
	 */
	public Boolean hasStringKey(String key) {
		return redisTemplate.hasKey(key);
	}


	public void setStringValue(String key, String value, long time, TimeUnit timeUnit) {
		redisTemplate.opsForValue().set(key, value, time, timeUnit);
	}

	public void rightPushAllList(String key, List<String> list) {
		redisTemplate.opsForList().rightPushAll(key, list);
	}


	public List<String> getRangeList(String key, int start, int end) {
		return redisTemplate.opsForList().range(key, start, end);
	}

	/**
	 * 获取Hash中的数据
	 *
	 * @param key Redis键
	 * @param hKey Hash键
	 * @return Hash中的对象
	 */
	public <T> T getHashValue(final String key, final String hKey)
	{
		HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
		return opsForHash.get(key, hKey);
	}

	public <T> void setHashValue(String key, String hKey, T value) {
		redisTemplate.opsForHash().put(key, hKey, value);
	}

	/**
	 * 设置set缓存
	 */
	public void setSetValue(String key, String value) {
		redisTemplate.opsForSet().add(key, value);
	}


	public void deleteCacheMapKey(String key, String hKey) {
		redisTemplate.opsForHash().delete(key, hKey);
	}

	/**
	 * 删除缓存
	 */
	public void deleteCache(String key) {
		redisTemplate.delete(key);
	}

	/**
	 * 判断hashKey是否存在
	 */
	public Boolean hasHashKey(String key, String hashKey){
		return redisTemplate.opsForHash().hasKey(key, hashKey);
	}

	/**
	 * 获取hashKey的个数
	 */
	public Long hashSize(String key){
		return redisTemplate.opsForHash().size(key);
	}

	/**
	 * 从zSet按score统计次数
	 */
	public Long zSetCount(String key, Double start, Double end){
		return redisTemplate.opsForZSet().count(key, start, end);
	}

	/**
	 * 往zSet中塞入数据
	 */
	public void zSetAdd(String key, String value, Double score){
		redisTemplate.opsForZSet().add(key, value, score);
	}

	/**
	 * zSet按照分数删除数据
	 */
	public void zSetRemoveRangeByScore(String key, Double start, Double end){
		redisTemplate.opsForZSet().removeRangeByScore(key, start, end);
	}

	/**
	 * 删除zSet某个member
	 */
	public void zSetRemove(String key, String value){
		redisTemplate.opsForZSet().remove(key, value);
	}

	/**
	 * 获取zSet的成员数
	 * @return
	 */
	public Long zSetSize(String key){
		return redisTemplate.opsForZSet().size(key);
	}

	public Long increment(String key) {
		return redisTemplate.opsForValue().increment(key);
	}

	/**
	 * 格式化缓存键，将各子键用 : 拼接起来
	 *
	 * @param subKeys
	 *            子键列表
	 * @return 缓存键
	 */
	public String formatKey(String... subKeys) {
		return String.join(":", subKeys);
	}



	public RedisConnectionFactory getConnectionFactory() {
		return redisTemplate.getConnectionFactory();
	}
}
